package com.zjy.software.day17;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FieldAndMethodDemo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //testFieldFunction();

        Class teacherClass = Teacher.class;
//        Method[] methods = teacherClass.getMethods();
        Method[] methods = teacherClass.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("method.getName() = " + method.getName());
            System.out.println("method.getModifiers() = " + method.getModifiers());
            System.out.println("method.getReturnType().getMethods() = " + method.getReturnType().getMethods());
        }

        Method method = teacherClass.getDeclaredMethod("eat", String.class);
        //
        //invoke是反射中用的最多的方法之一,用来动态的调用一个类中的方法
        Teacher teacher = new Teacher();
        method.invoke(teacher, "apple");

    }

    private static void testFieldFunction() throws NoSuchFieldException, IllegalAccessException {
        Class personClass = Person.class;
        //获取当前类或者父类的所有的public字段
        Field[] fields = personClass.getFields();
        for (Field field : fields) {
            System.out.println("field = " + field);
        }

        //可以获取本类的私有字段
        Field ageField = personClass.getDeclaredField("name");
        System.out.println("ageField = " + ageField);

        //获取字段的属性
        System.out.println("ageField.getName() = " + ageField.getName());
        System.out.println("ageField.getModifiers() = " + ageField.getModifiers());
        System.out.println("ageField.getType() = " + ageField.getType());

        //设置字段的值
        Person person = new Person();
        ageField.set(person,"zhangSan");
        System.out.println("person = " + person);
        System.out.println("ageField.get(person) = " + ageField.get(person));
    }
}
class Person{
    public Person(){

    }
    public Person(String name){
        this.name=name;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (getAge() != person.getAge()) return false;
        return getName() != null ? getName().equals(person.getName()) : person.getName() == null;
    }

    @Override
    public int hashCode() {
        int result = getName() != null ? getName().hashCode() : 0;
        result = 31 * result + getAge();
        return result;
    }

    public String name;
    private int age;
}
class Teacher extends Person implements Runnable{
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "address='" + address + '\'' +
                ", number='" + number + '\'' +
                '}';
    }

    private String address;
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Teacher teacher = (Teacher) o;

        return getNumber() != null ? getNumber().equals(teacher.getNumber()) : teacher.getNumber() == null;
    }

    @Override
    public int hashCode() {
        return getNumber() != null ? getNumber().hashCode() : 0;
    }

    public String number;

    public void eat(String food){
        System.out.println("老师在吃:"+food+"");
    }

    @Override
    public void run() {
        System.out.println("老师在跑步");
    }

    private void look(){
        System.out.println("老师在看");
    }
}